//
//  RootViewController.m
//  UI_元旦豆瓣
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 31付冬梅. All rights reserved.
//

#import "RootViewController.h"
#import "ActivityTableViewCell.h"
#import "ActivityModel.h"
#import "UIImageView+WebCache.h"
#import "ActivityDetailsViewController.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *activityArr;
@property (nonatomic, retain) NSMutableArray *modelArr;
@end

@implementation RootViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"电影";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:55 / 255.0 green:90 / 255.0 blue:110 /255.0 alpha:0.8];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item1;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self dataHandls];
    [self.tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"activityCell"];
    
    
}
- (void)dataHandls {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activity" ofType:@"plist"];
    self.activityArr = [NSMutableArray arrayWithContentsOfFile:path];
    self.modelArr = [NSMutableArray array];
    for (NSDictionary *dic in _activityArr) {
        ActivityModel *model = [ActivityModel activityModel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArr addObject:model];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 260;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _activityArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activityCell"];
    ActivityModel *model = _modelArr[indexPath.row];
    cell.titleLabel.text = model.title;
    NSString *str1 = model.begin_time;
    NSString *str2 = model.end_time;
    NSString *str3 = [str1 substringWithRange:NSMakeRange(5, 11)];
    NSString *str4 = [str2 substringWithRange:NSMakeRange(5, 11)];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@ -- %@", str3, str4];
    cell.addressLabel.text = model.address;
    cell.category_nameLabel.text = [NSString stringWithFormat:@"类型:  %@", model.category_name];
    cell.participant_countLabel.text = [NSString stringWithFormat:@"喜欢:  %@", [model.participant_count stringValue]];
    cell.wisher_countLabel.text = [NSString stringWithFormat:@"参加:  %@", [model.wisher_count stringValue]];
    [cell.activityView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    
    return cell;
}
- (void)itemAction:(UIBarButtonItem *)item {
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityDetailsViewController *activityDetails = [[ActivityDetailsViewController alloc] init];
    ActivityModel *model = _modelArr[indexPath.row];
    activityDetails.str = model.title;
    activityDetails.title1 = model.title;
    NSString *str1 = model.begin_time;
    NSString *str2 = model.end_time;
    NSString *str3 = [str1 substringWithRange:NSMakeRange(5, 11)];
    NSString *str4 = [str2 substringWithRange:NSMakeRange(5, 11)];
    activityDetails.time = [NSString stringWithFormat:@"%@ -- %@", str3, str4];
    activityDetails.type = model.category_name;
    activityDetails.address = model.address;
    activityDetails.activityView = model.image;
//    [activityDetails.activityView sd_setImageWithURL:[NSURL URLWithString:model.image]];
//    NSLog(@"%@", activityDetails.activityView);
//    NSDictionary *dic = self.activityArr[indexPath.row];
    
//    activityDetails.Dic = dic;
    
    
    [self.navigationController pushViewController:activityDetails animated:YES];
    
    activityDetails.strDetails = model.content;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
